<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'accept:supply:pmWatersupply:edit'"
    @register="registerDrawer"
    okText="确认验收"
    @ok="submitDrawer"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
    <!-- <a-button type="primary" @click="submitDrawer">提交</a-button> -->
    <!-- <template #footer>
      <a-button type="primary" @click="submitDrawer">取消</a-button>
      <a-button type="primary" @click="handleSubmit">确认</a-button>
      <a-button type="primary" @click="submitDrawer">提交</a-button>
    </template> -->
    <!-- v-if="record.value.status == 9" -->
    <!-- <template #appendFooter>
      <a-button type="warning" v-auth="'accept:supply:pmWatersupply:edit'" @click="submitDrawer" v-if="getBpushed.value != 1">保存并推送报装</a-button>
    </template> -->
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsAcceptSupplyPmWatersupplyForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import {
    PmWatersupply,
    pmWatersupplySave,
    pmWatersupplyForm,
    pmWatersupplySubmit,
  } from '/@/api/accept/supply/pmWatersupply';

  import { pmProjectinformationPreTreeData } from '/@/api/proj/info/pmProjectinformation';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('accept.supply.pmWatersupply');
  const { showMessage } = useMessage();
  const record = ref<PmWatersupply>({} as PmWatersupply);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增竣工验收') : t('编辑竣工验收'),
  }));

  const getBpushed = computed(() => ({
    value: record.value.bpushed,
  }));

  const getStatus = computed(() => ({
    value: record.value.status,
  }));

  const inputFormSchemas: FormSchema[] = [
    // {
    //   label: t('项目名称'),
    //   field: 'projectno',
    //   fieldLabel: 'name',
    //   component: 'TreeSelect',
    //   componentProps: {
    //     api: pmProjectinformationPreTreeData,
    //     params: {},
    //     canSelectParent: false,
    //     allowClear: false,
    //     // disabled: computed(() => record.value.projectname !== null),
    //   },
    //   required: true,
    //   ifShow: (e) => {
    //     console.log(e, 'e====+++++');
    //     return e.model.name == null ? true : false;
    //   },
    // },
    {
      label: t('项目名称'),
      field: 'projectname',
      fieldLabel: 'name',
      component: 'Input',
      componentProps: {
        disabled: true,
      },
      required: true,
      // ifShow: (e) => {
      //   console.log(e, 'e====');
      //   return e.model.projectname !== null ? true : false;
      // },
    },
    {
      label: t('项目编号'),
      field: 'projectno',
      component: 'Input',
      componentProps: {
        maxlength: 50,
        disabled: true,
      },
      required: true,
    },
    {
      label: t('验收日期'),
      field: 'checktime',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD',
      },
      required: true,
      // defaultValue 为年月日,
      defaultValue: getNowFormatDate(),
    },
    {
      label: t('验收结果'),
      field: 'checkresult',
      component: 'RadioGroup',
      componentProps: {
        dictType: 'zf_check_result',
      },
      required: true,
    },
    {
      label: t('不合格原因'),
      field: 'remarks',
      component: 'InputTextArea',
      componentProps: {
        maxlength: 255,
      },
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('现场图片'),
      field: 'dataMap',
      component: 'Upload',
      componentProps: {
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'PmWaterSupply_xc_image',
        uploadType: 'all',
      },
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('验收图片'),
      field: 'dataMap',
      component: 'Upload',
      componentProps: {
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'PmWaterSupply_ys_image',
        uploadType: 'all',
      },
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('补充资料'),
      field: 'dataMap',
      component: 'Upload',
      componentProps: {
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'PmWaterSupply_more_file',
        uploadType: 'all',
      },
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('发起人名称'),
      field: 'createByName',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
      show: false,
    },
    {
      label: t('发起人编码'),
      field: 'createBy',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
      show: false,
    },
    {
      label: t('是否推送报装'),
      field: 'bpushed',
      component: 'Input',
      componentProps: {
        maxlength: 10,
      },
      show: false,
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate, updateSchema }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 12, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();

    const res = await pmWatersupplyForm(data);
    record.value = (res.pmWatersupply || {}) as PmWatersupply;
    record.value.__t = new Date().getTime();
    record.value.projectname = record.value.projectname
      ? record.value.projectname
      : data.projectname
      ? data.projectname
      : record.value.name;
    console.log('data===', data, record.value);
    setFieldsValue(record.value);
    setDrawerProps({ showOkBtn: record.value.bpushed != '1' });
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      // console.log('submit', params, data, record);
      const res = await pmWatersupplySave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
  async function submitDrawer() {
    console.log('submitDrawer');
    // const res = await pmWatersupplySubmit(record);
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      // console.log('submit', params, data, record);
      const res = await pmWatersupplySubmit(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
  // 当前时间年月日
  function getNowFormatDate() {
    var date = new Date();
    var seperator1 = '-';
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
      month = '0' + month;
    }
    if (strDate >= 0 && strDate <= 9) {
      strDate = '0' + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;
    return currentdate;
  }
</script>
